/**
 * 
 */
package sudoku.util.game;

import java.io.FileWriter;
import java.io.PrintWriter;

import javax.swing.JOptionPane;

import sudoku.model.SudokuModel;
import sudoku.ui.SudokuUI;
import sudoku.util.Config;
import sudoku.util.Language;


/**
 * @author Romet
 *
 */
public class PrintPreview {

	private volatile SudokuModel model;
	private volatile Config config;
	private volatile SudokuUI m2ng;
	private volatile Language language;
	
	public PrintPreview(Language language, SudokuModel model, Config config, SudokuUI m2ng){
		this.language = language;
		this.model = model;
		this.config = config;
		this.m2ng = m2ng;

		kirjutaHTML();
	}
	
	public void kirjutaHTML(){
		try {
			PrintWriter writer = new PrintWriter(new FileWriter(model.getNimi()+".html"));
			writer.println("<html>");
			writer.println("<head>");
			writer.println("<title>"+model.getNimi()+"</title>");
			writer.println("</head>");
			writer.println("<body>");
			writer.println("<table border=3 rules=all>");
			writer.println("<tbody>");
			for (int i = 0; i < config.getKast(); i++) {
				writer.println("<tr>");
				for (int j = 0; j < config.getKast(); j++) {
					writer.println("<td>");
					writer.println("<table border=3 rules=all>");
					writer.println("<tbody>");
					for (int k=(i*config.getKast()); k<(config.getKast()*(i+1)); k++){
						writer.println("<tr>");
						for (int l=(j*config.getKast()); l<(config.getKast()*(j+1)); l++){
							if (!model.getSudokuButtons()[k][l].isAbiNupp()){
								writer.println("<td width=45 height=50 align=center><b>"+model.getSudokuButtons()[k][l].getNumber()+"</b></td>");
							}
							else {
								writer.println("<td width=45 height=50 align=center><b> </b></td>");
							}
						}
						writer.println("</tr>");
					}
					writer.println("</tbody>");
					writer.println("</table>");
					writer.println("</td>");
				}
				writer.println("</tr>");
			}
			writer.println("</tbody>");
			writer.println("</table>");
			writer.println("</body>");
			writer.println("</html>");
			writer.close();
			JOptionPane.showMessageDialog(m2ng, this.language.getPrintPreview_message(model.getNimi()));
		} catch (Exception e) {
			Config.log(e.getClass().toString()+": "+e.getMessage(), e.getStackTrace());
		}
	}
}
